package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;


    @Override
    public BusinessDataVO businessData() {
        LocalDate now = LocalDate.now();

        Double turnover=orderMapper.getTurnoverByDate(now);//营业额
        Integer validOrderCount  =orderMapper.getCountOrdersNumber(5,now);//有效订单数
        Integer orderCount=orderMapper.getCountOrdersNumber(null,now);
        Double orderCompletionRate=validOrderCount.doubleValue()/orderCount.doubleValue();//订单完成率
        Double unitPrice=turnover/validOrderCount.doubleValue();//平均客单价
        unitPrice=BigDecimal.valueOf(unitPrice)
                .setScale(2, RoundingMode.HALF_UP) // 保留2位，四舍五入
                .doubleValue();
        Integer newUsers=userMapper.getNewUserByData(now);//新增用户数

        //数据封装
        BusinessDataVO result=new BusinessDataVO();
        result.setNewUsers(newUsers);
        result.setValidOrderCount(validOrderCount);
        result.setTurnover(turnover);
        result.setOrderCompletionRate(orderCompletionRate);
        result.setUnitPrice(unitPrice);
        return result;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer sold=setmealMapper.getCountByStatus(1);
        Integer discontinued=setmealMapper.getCountByStatus(0);

        SetmealOverViewVO  result=new SetmealOverViewVO();
        result.setSold(sold);
        result.setDiscontinued(discontinued);
        return result;
    }

    @Override
    public DishOverViewVO overviewDishes() {
        Integer sold=dishMapper.getCountByStatus(1);
        Integer discontinued=dishMapper.getCountByStatus(0);

        DishOverViewVO  result=new DishOverViewVO();
        result.setSold(sold);
        result.setDiscontinued(discontinued);
        return result;
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        OrderOverViewVO result= orderMapper.getOverviewOrders();
        return result;
    }
}
